package com.sduept.nwld.dataserver.controller.daily;

import com.sduept.bigdata.weather.map.MapDataCache;
import com.sduept.nwld.dataserver.manager.protection.ProtectOverhaulManager;
import com.sduept.nwld.dataserver.model.daily.OverhaulMsg;
import org.primefaces.event.CellEditEvent;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Named
@ViewScoped
public class OverhaulmagController extends DailyReportAbstractController {

	/**
	 * 保护检修管理controller类
	 */
	private static final long serialVersionUID = 4797459228239626554L;

	@Autowired
	private ProtectOverhaulManager po;
	@Autowired
	private MapDataCache cim;

	private OverhaulMsg selectOverhaul = null;
	private OverhaulMsg created = null;
	private OverhaulMsg edit = null;
	private List<OverhaulMsg> overhaul = new ArrayList<>();
	private String queryTxt = "";

	@PostConstruct
	private void init() {
		overhaul = po.getOverhaulByTimeAndState(queryDate1, queryDate2, "全部");
	}

	public void create() {
		created = new OverhaulMsg();
		created = po.creatOverhaulMag(created);
		overhaul.add(0, created);
		created = po.updateOverhaulMsgBySatus(created);
	}

	public void onRowSelect(SelectEvent event) {
		selectOverhaul = (OverhaulMsg) event.getObject();
	}

	public void deleteOverhaul() {
		if (selectOverhaul != null) {
			po.delOverhaulMag(selectOverhaul);
			overhaul.remove(selectOverhaul);
			addSuccessMessage("删除成功!");
			selectOverhaul = null;
		} else {
			addErrorMessage("请选择一条数据进行删除");
		}
	}

	/**
	 * 单元格编辑
	 * 
	 * @param event
	 */
	public void overhaulCellEdit(CellEditEvent event) {
		selectOverhaul = null;
		for (OverhaulMsg k : overhaul) {
			if (k.getId().equals(event.getRowKey())) {
				selectOverhaul = k;
				break;
			}
		}
		if (selectOverhaul != null) {
			if (timeIsChange(selectOverhaul)) {
				selectOverhaul = po.updateOverhaulMsgBySatus(selectOverhaul);
			}
			po.updateOverhaulMag(selectOverhaul);

		}
		addSuccessMessage("修改成功!");
	}

	/**
	 * 行编辑
	 * 
	 * @param event
	 */
	public void onRowEdit(RowEditEvent event) {
		selectOverhaul = (OverhaulMsg) event.getObject();
		if (selectOverhaul != null) {
			if (timeIsChange(selectOverhaul)) {
				selectOverhaul = po.updateOverhaulMsgBySatus(selectOverhaul);
			}
			po.updateOverhaulMag(selectOverhaul);

		}
		addSuccessMessage("修改成功!");
	}

	/**
	 * 判断检修开始时间或结束时间有没有改变
	 * 
	 * @param o
	 * @return
	 */
	public boolean timeIsChange(OverhaulMsg selectOverhaul) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		boolean isChannge = false;
		OverhaulMsg k = po.getOverhaulById(selectOverhaul.getId());
		String start = format.format(k.getStartTime());
		String startSelsct = format.format(selectOverhaul.getStartTime());
		String end = format.format(k.getEndTime());
		String endSelect = format.format(selectOverhaul.getEndTime());
		if ((!start.equals(startSelsct)) || (!end.equals(endSelect))) {
			isChannge = true;
		}
		return isChannge;
	}


	public void resetQuery() {
		queryDate1 = null;
		queryDate2 = null;
		resetqueryDate();
		queryTxt = "全部";
		queryByTimeAndState();
		selectOverhaul = null;
		created = null;
		edit = null;

	}

	public void queryByTimeAndState() {
		overhaul = po.getOverhaulByTimeAndState(queryDate1, queryDate2, queryTxt);
		refreshQueryParamter();
		selectOverhaul = null;
		created = null;
	}

	public List<OverhaulMsg> getOverhaul() {
		return overhaul;
	}

	public void setOverhaul(List<OverhaulMsg> overhaul) {
		this.overhaul = overhaul;
	}

	public OverhaulMsg getSelectOverhaul() {
		return selectOverhaul;
	}

	public void setSelectOverhaul(OverhaulMsg selectOverhaul) {
		this.selectOverhaul = selectOverhaul;
	}

	public OverhaulMsg getCreated() {
		return created;
	}

	public void setCreated(OverhaulMsg created) {
		this.created = created;
	}

	public OverhaulMsg getEdit() {
		return edit;
	}

	public void setEdit(OverhaulMsg edit) {
		this.edit = edit;
	}

	public String getQueryTxt() {
		return queryTxt;
	}

	public void setQueryTxt(String queryTxt) {
		this.queryTxt = queryTxt;
	}

}
